package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.myapplication.activity.PlayListDetailActivity;
import com.example.myapplication.R;
import com.example.myapplication.entity.PlayList;

import java.util.List;

public class MineIndexHorAdapter extends RecyclerView.Adapter<MineIndexHorAdapter.MineIndexHorHolder>{
    private List<PlayList> playlistBeanList;
    private Context context;

    public void setData(List<PlayList> playlistBeanList,Context context){
        this.context = context;
        this.playlistBeanList = playlistBeanList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MineIndexHorHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler_hor,parent,false);
        return new MineIndexHorHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MineIndexHorHolder holder, int position) {
        PlayList playlistBean = playlistBeanList.get(position);
        holder.tv_name.setText(playlistBean.getName());
        RoundedCorners roundedCorners= new RoundedCorners(30);
        RequestOptions options= RequestOptions.bitmapTransform(roundedCorners).override(300, 300);
        Glide.with(context).load(playlistBean.getCoverImgUrl())
                .centerCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .error(R.drawable.ic_baseline_refresh_24)
                .placeholder(R.drawable.ic_baseline_refresh_24)
                .apply(options)
                .into(holder.img_recycler_hor);
        holder.view_pagers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PlayListDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("playList", String.valueOf(playlistBean.getId()));
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return playlistBeanList==null?0:playlistBeanList.size();
    }


    class MineIndexHorHolder extends RecyclerView.ViewHolder {
        ImageView img_recycler_hor;
        TextView tv_name;
        View view_pagers;
        public MineIndexHorHolder(@NonNull View itemView) {
            super(itemView);
            img_recycler_hor = itemView.findViewById(R.id.img_recycler_hor);
            tv_name = itemView.findViewById(R.id.tv_name);
            view_pagers = itemView.findViewById(R.id.view_pagers);
        }
    }
}
